package util;

import java.util.HashMap;
import java.util.Map.Entry;

import android.content.Context;

import object.Command;
import object.Command.CommandType;

public class CommandManager {
	private static CommandManager manager;
	public HashMap<CommandType, Command> commandsList;
	
	private CommandManager(Context context) {
        initializeCommands();
        addCommands(context);
	}
	
	public static CommandManager getInstance(Context context) {
		if (manager == null) {
			manager = new CommandManager(context);
	        return manager;
		}
		else return manager;
	}
	
	private void initializeCommands()
    {
        commandsList = new HashMap<CommandType, Command>();
        for (CommandType commandType : CommandType.values()) {
        	commandsList.put(commandType, new Command());
        }
    }
	    
    private void addCommands(Context context)
    {
    	for (Entry<CommandType, Command> entry : commandsList.entrySet()) {
    	    entry.setValue(XMLParser.GetCommandList(entry.getKey(), context.getResources()));
    	}
    }
    
    public void executeCommand(Context context, CommandType command, String data)
    {
    	switch (command)
    	{
    		case Call:
    			TaskHelper.startCall(context, data);
    			break;
    		case SMS:
    			TaskHelper.sendSMS(context, data);
        		break;
    		case Alarm:
    			if (data.isEmpty()) data="0";
    			TaskHelper.setAlarm(context, Integer.parseInt(data), 0);
        		break;
    		case Address:
    			TaskHelper.searchAddress(context, data);
        		break;
    		case Location:
    			TaskHelper.getCurrentLocation(context);
        		break;
    		case Skype:
    			TaskHelper.openCallWithSkype(context, data);
        		break;
    		case Mail:
    			TaskHelper.sendMailToAddress(context, data);
        		break;
    		case Facebook:
    			TaskHelper.openFacebook(context);
        		break;
    		case Web:
    			TaskHelper.openWebBrowser(context);
        		break;
    		case Wifi:
    			TaskHelper.toggleWifi(context);
        		break;
    		case Cellular:
    			TaskHelper.toggle3G(context);
        		break;
    		case Bluetooth:
    			TaskHelper.toggleBluetooth(context);
        		break;
    		case Camera:
    			TaskHelper.openCamera(context);
        		break;
    		case VideoCamera:
    			TaskHelper.openVideoCamera(context);
        		break;
    		case MusicPlayer:
    			TaskHelper.openMusicPlayer(context);
        		break;
    		case VideoPlayer:
    			TaskHelper.openVideoPlayer(context);
        		break;
    		case ImageViewer:
    			TaskHelper.openImageViewer(context);
        		break;
    		case Applications:
    			TaskHelper.openApplications(context, "");
        		break;
    	}
    }
}
